import functools


def map_frequency(text):
    words = text.split(" ")
    frequency = {}
    for word in words:
        if word in frequency:
            # 如果频率字典中有这个词，那么在计数中加上1
            frequency[word] += 1
        else:
            # 如果频率字段中没有该单词，则将其计数设置为1
            frequency[word] = 1
    return frequency


def merge_dict(first, second):
    merged = first
    for key in second:
        if key in merged:
            # 如果单词在两个字典中同时存在，则合并频率计数
            merged[key] = merged[key] + second[key]
        else:
            # 如果单词不同时在两个字典中，则复制频率计数。
            merged[key] = second[key]
    return merged


lines = [
    "I know what I know",
    "I know that I know",
    "I don't know much",
    "They don't know much"
]
# 对于每一行文本，执行map操作
mapped_results = [map_frequency(line) for line in lines]

for result in mapped_results:
    print(result)

# 将所有的中间频率计数规约为一个结果
print(functools.reduce(merge_dict, mapped_results))
